package com.sfddata.musicpillow.user.service.impl;

import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfddata.musicpillow.common.utils.PageUtils;
import com.sfddata.musicpillow.common.utils.Query;

import com.sfddata.musicpillow.user.dao.QuestionnaireInfoDao;
import com.sfddata.musicpillow.user.entity.QuestionnaireInfoEntity;
import com.sfddata.musicpillow.user.service.QuestionnaireInfoService;


@Service("questionnaireInfoService")
public class QuestionnaireInfoServiceImpl extends ServiceImpl<QuestionnaireInfoDao, QuestionnaireInfoEntity> implements QuestionnaireInfoService {




    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<QuestionnaireInfoEntity> page = this.page(
                new Query<QuestionnaireInfoEntity>().getPage(params),
                new QueryWrapper<QuestionnaireInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Integer countSleepScore(List<String> list) {
        // 睡眠质量 6-14
        // 6 7 8 9 a b c d e f g   h  i  j 11 12 13 14
        // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
        Integer A = (list.get(14).equals("A")) ? 0:(list.get(14).equals("B") ) ? 1:(list.get(14).equals("C") ) ? 2:3;

        Integer B1 = (Integer.valueOf(list.get(1)) <= 15) ? 0:(Integer.valueOf(list.get(1)) <= 30) ? 1:(Integer.valueOf(list.get(1)) <= 60) ? 2:3;
        Integer B2 = (list.get(4).equals("A") ) ? 0:(list.get(4).equals("B") ) ? 1:(list.get(4).equals("C") ) ? 2:3;
        Integer B = (B1+B2 < 1) ? 0:(B1+B2 < 3) ? 1:(B1+B2 < 5) ? 2:3;

        Integer C = (Integer.valueOf(list.get(3)) > 7) ? 0:(Integer.valueOf(list.get(3)) > 6) ? 1:(Integer.valueOf(list.get(3)) > 5) ? 2:3;

        Integer D = Integer.valueOf(list.get(3))/(Integer.valueOf(list.get(2))-Integer.valueOf(list.get(0)));

        Integer E1 = (list.get(5).equals("A") ) ? 0:(list.get(5).equals("B") ) ? 1:(list.get(5).equals("C") ) ? 2:3;
        Integer E2 = (list.get(6).equals("A") ) ? 0:(list.get(6).equals("B") ) ? 1:(list.get(6).equals("C") ) ? 2:3;
        Integer E3 = (list.get(7).equals("A") ) ? 0:(list.get(7).equals("B") ) ? 1:(list.get(7).equals("C") ) ? 2:3;
        Integer E4 = (list.get(8).equals("A") ) ? 0:(list.get(8).equals("B") ) ? 1:(list.get(8).equals("C") ) ? 2:3;
        Integer E5 = (list.get(9).equals("A") ) ? 0:(list.get(9).equals("B") ) ? 1:(list.get(9).equals("C") ) ? 2:3;
        Integer E6 = (list.get(10).equals("A") ) ? 0:(list.get(10).equals("B") ) ? 1:(list.get(10).equals("C") ) ? 2:3;
        Integer E7 = (list.get(11).equals("A") ) ? 0:(list.get(11).equals("B") ) ? 1:(list.get(11).equals("C") ) ? 2:3;
        Integer E8 = (list.get(12).equals("A") ) ? 0:(list.get(12).equals("B") ) ? 1:(list.get(12).equals("C") ) ? 2:3;
        Integer E9 = (list.get(13).equals("A") ) ? 0:(list.get(13).equals("B") ) ? 1:(list.get(13).equals("C") ) ? 2:3;

        Integer E = (E1+E2+E3+E4+E5+E6+E7+E8+E9 < 1) ? 0:(E1+E2+E3+E4+E5+E6+E7+E8+E9 < 10) ? 1:(E1+E2+E3+E4+E5+E6+E7+E8+E9 < 19) ? 2:3;

        Integer F = (list.get(15).equals("A") ) ? 0:(list.get(15).equals("B") ) ? 1:(list.get(15).equals("C") ) ? 2:3;

        Integer G1 = (list.get(16).equals("A") ) ? 0:(list.get(16).equals("B") ) ? 1:(list.get(16).equals("C") ) ? 2:3;
        Integer G2 = (list.get(17).equals("A") ) ? 0:(list.get(17).equals("B") ) ? 1:(list.get(17).equals("C") ) ? 2:3;

        Integer G = (G1+G2 < 1) ? 0:(G1+G2 < 3) ? 1:(G1+G2 < 5) ? 2:3;
        // 总分
        Integer PSQI = A+B+C+D+E+F+G;
        if ( PSQI < 3 ){
            return 41;
        }else if ( PSQI < 6 ){
            return 32;
        }else if ( PSQI < 9 ){
            return 28;
        }else if ( PSQI < 11 ){
            return 23;
        }else if ( PSQI < 16 ){
            return 14;
        }else if ( PSQI < 19 ){
            return 8;
        }else{
            return 3;
        }
    }

    @Override
    public Integer countPressScore(Map list) {

        // 25 26  27 28 29 30 31 32 33 34
        //  0  1  2  3  4  5  6  7  8  9
        Integer CPSS = 0;

        List<Integer> seq = Arrays.asList(25, 26, 27, 30, 33, 34);
        for (int i=25; i<35; i++){
            if ( seq.contains(i) ){
                CPSS += (list.get(i).equals("A") ) ? 0:(list.get(i).equals("B") ) ? 1:(list.get(i).equals("C") ) ? 2:(list.get(i).equals("D") ) ? 3:4;
            }else {
                CPSS += (list.get(i).equals("A") ) ? 4:(list.get(i).equals("B") ) ? 3:(list.get(i).equals("C") ) ? 2:(list.get(i).equals("D") ) ? 1:0;
            }
        }
        return CPSS;
    }


    @Override
    public Double countEatingScore(Map list) {



        // 35 37 38 39 40 41
        // 0  1  2  3  4  5
        // eat部分 35-41 缺36
        Integer EAT = 0;
        List<Integer> seq = Arrays.asList(35, 39, 40, 41);
        for (int i=35; i<42; i++){
            if (i == 36){
                continue;
            }
            if ( i == 39 ){
                // 若题39中选A，跳过40题则在40题中的得分为3分

                if (list.get(i).equals("A") ){
                    EAT += 6;
                    i += 1;
                    continue;
                }

            }else if ( seq.contains(i) ){



                EAT += (list.get(i).equals("A") ) ? 3:(list.get(i).equals("B") ) ? 2:(list.get(i).equals("C") ) ? 1:0;

            }else if (i == 37){
                // 37
                String s = (String) list.get(i);
                List<String> collect = Stream.iterate(0, n -> ++n).limit(s.length()).map(n -> "" + s.charAt(n)).collect(Collectors.toList());
                int size = collect.size();
                for (int num=0; num < size; num++){
                    EAT += (collect.get(num).equals("A") ) ? -1:(collect.get(num).equals("B") ) ? -1:(collect.get(num).equals("C") ) ? 1:1;
                }
            }else {
                // 38
                String s = (String) list.get(i);
                List<String> collect = Stream.iterate(0, n -> ++n).limit(s.length()).map(n -> "" + s.charAt(n)).collect(Collectors.toList());
                int size = collect.size();
                for (int num=0; num < size; num++){
                    EAT += (collect.get(num).equals("A") ) ? 2:(collect.get(num).equals("B") ) ? -1:(collect.get(num).equals("C") ) ? 1:
                            (collect.get(num).equals("D") ) ? -1:(collect.get(num).equals("E") ) ? 1:(collect.get(num).equals("F") ) ? -1:2;
                }
            }
        }

        double eatFinal = 0.0;
        if ( EAT < 1 ){
            eatFinal = 0.5;
        }else if ( EAT < 9 ){
            eatFinal = 1;
        }else if ( EAT < 12 ){
            eatFinal = 1.5;
        }else if ( EAT < 24 ){
            eatFinal = 3;
        }else if ( EAT < 26 ){
            eatFinal = 4.5;
        }else{
            eatFinal = 6;
        }

        return eatFinal;
    }

    @Override
    public List<String> countShowlist(Integer psqi, Integer cpss, Map maps) throws ParseException {

        List<String> show = new ArrayList<>();
        // 1-4 睡眠质量
        if (psqi < 6){
            show.add("1");
        }else if (psqi < 11){
            show.add("2");
        }else if (psqi < 16){
            show.add("3");
        }else {
            show.add("4");
        }
        // 5 睡眠时间偏短
        double sleepTime = Double.parseDouble((String) maps.get("10"));

        if (sleepTime < 4){
            show.add("5");
        }
        // 6 睡眠效率低
        Integer D = countSleepEfficiency((String)maps.get("7"), (String)maps.get("9"), (String)maps.get("10"));
        if (D != 0){
            show.add("6");
        }
        // 7 夜间易醒
        if ( maps.get("12").equals("D") ){
            show.add("7");
        }
        // 8 做噩梦
        if ( maps.get("18").equals("D") ){
            show.add("8");
        }
        // 9 日间功能障碍
        if ( maps.get("23").equals("D") || maps.get("24").equals("D") ){
            show.add("9");
        }
        // 10-12 压力
        if (cpss < 10){
            show.add("10");
        }else if (cpss < 26){
            show.add("11");
        }else {
            show.add("12");
        }
        // 13 咖啡问题
        String answer38 = (String)maps.get("38");
        if ( answer38.contains("B") || answer38.contains("C") ){
            show.add("13");
        }
        // 14 抽烟问题
        if ( maps.get("41").equals("D") || maps.get("41").equals("C") ){
            show.add("14");
        }
        // 15 喝酒问题
        if (maps.containsKey("40")){
            if ( maps.get("39").equals("D") || maps.get("39").equals("C") ||
                    maps.get("40").equals("D") || maps.get("40").equals("C") || maps.get("40").equals("B") ){
                show.add("15");
            }
        }else{
            if ( maps.get("39").equals("D") || maps.get("39").equals("C")){
                show.add("15");
            }
        }

        // 16 运动问题
        if ( maps.get("42").equals("A") ||
                (maps.get("42").equals("B") && maps.get("33").equals("A") && (maps.get("44").equals("A") || maps.get("44").equals("B"))) ){
            show.add("16");
        }
        // 17 运动过晚
        if ( maps.get("45").equals("D") ){
            show.add("17");
        }
        String answer46 = (String) maps.get("46");
        // 18 双脚冰冷
        if ( answer46.contains("A") ){
            show.add("18");
        }
        // 19 感冒
        if ( answer46.contains("B") ){
            show.add("19");
        }
        // 20 腰椎问题
        if ( answer46.contains("C") || answer46.contains("D")){
            show.add("20");
        }
        // 21 打鼾
        if ( answer46.contains("E") || maps.get("15").equals("D")){
            show.add("21");
        }
        return show;
    }

    @Override
    public Integer countBodyScore(Map list) {

        // body 部分 4.1身高 4.2体重 46 36
        Integer body1;
        Double BMI = Double.parseDouble((String) list.get("4b"))/Double.parseDouble((String) list.get("4a")) * Double.parseDouble((String) list.get("4a")) ;
        if (BMI <= 23.9 && 18.5 <= BMI){
            body1 = 1;
        }else {
            body1 = 0;
        }
        Integer body2 = (list.get("36").equals("A") ) ? 3:(list.get("36").equals("B") ) ? 2:(list.get("36").equals("C") ) ? 1:0;; //26
        Integer body3;
        if (list.get("46").equals("H")){
            body3 = 7;
        }else {
            String answer = (String) list.get("46");
            String[] split = answer.split("");
            if (split.length <= 3){
                body3 = 4;
            }else if (split.length <= 6){
                body3 = 1;
            }else {
                body3 = 0;
            }
        }
        return body1 + body2 + body3;
    }

    @Override
    public Integer countHealthSleepScore(Map list) throws ParseException {
        // sleepList
        // 7 8 9 10 11 12 13  21
        // 0 1 2 3  4         "21"
        Integer A = (list.get("21").equals("A")) ? 0:(list.get("21").equals("B") ) ? 1:(list.get("21").equals("C") ) ? 2:3;

        Integer B1 = (list.get("8").equals("A")) ? 0:(list.get("8").equals("B")) ? 1:(list.get("8").equals("C")) ? 2:3;
        Integer B2 = (list.get("11").equals("A") ) ? 0:(list.get("11").equals("B") ) ? 1:(list.get("11").equals("C") ) ? 2:3;
        Integer B = (B1+B2 < 1) ? 0:(B1+B2 < 3) ? 1:(B1+B2 < 5) ? 2:3;

        Integer C = (Double.parseDouble((String) list.get("10")) > 7 ) ? 0:(Double.parseDouble((String) list.get("10")) > 6) ? 1:(Double.parseDouble((String) list.get("10")) > 5) ? 2:3;
        Integer D = countSleepEfficiency((String)list.get("7"), (String)list.get("9"), (String)list.get("10"));
        Integer E1 = (list.get("12").equals("A") ) ? 0:(list.get("12").equals("B") ) ? 1:(list.get("12").equals("C") ) ? 2:3;
        Integer E2 = (list.get("13").equals("A") ) ? 0:(list.get("13").equals("B") ) ? 1:(list.get("13").equals("C") ) ? 2:3;
        Integer E3 = (list.get("14").equals("A") ) ? 0:(list.get("14").equals("B") ) ? 1:(list.get("14").equals("C") ) ? 2:3;
        Integer E4 = (list.get("15").equals("A") ) ? 0:(list.get("15").equals("B") ) ? 1:(list.get("15").equals("C") ) ? 2:3;
        Integer E5 = (list.get("16").equals("A") ) ? 0:(list.get("16").equals("B") ) ? 1:(list.get("16").equals("C") ) ? 2:3;
        Integer E6 = (list.get("17").equals("A") ) ? 0:(list.get("17").equals("B") ) ? 1:(list.get("17").equals("C") ) ? 2:3;
        Integer E7 = (list.get("18").equals("A") ) ? 0:(list.get("18").equals("B") ) ? 1:(list.get("18").equals("C") ) ? 2:3;
        Integer E8 = (list.get("19").equals("A") ) ? 0:(list.get("19").equals("B") ) ? 1:(list.get("19").equals("C") ) ? 2:3;
        Integer E9 = (list.get("20").equals("A") ) ? 0:(list.get("20").equals("B") ) ? 1:(list.get("20").equals("C") ) ? 2:3;

        Integer E = (E1+E2+E3+E4+E5+E6+E7+E8+E9 < 1) ? 0:(E1+E2+E3+E4+E5+E6+E7+E8+E9 < 10) ? 1:(E1+E2+E3+E4+E5+E6+E7+E8+E9 < 19) ? 2:3;

        Integer F = (list.get("22").equals("A") ) ? 0:(list.get("22").equals("B") ) ? 1:(list.get("22").equals("C") ) ? 2:3;

        Integer G1 = (list.get("23").equals("A") ) ? 0:(list.get("23").equals("B") ) ? 1:(list.get("23").equals("C") ) ? 2:3;
        Integer G2 = (list.get("24").equals("A") ) ? 0:(list.get("24").equals("B") ) ? 1:(list.get("24").equals("C") ) ? 2:3;

        Integer G = (G1+G2 < 1) ? 0:(G1+G2 < 3) ? 1:(G1+G2 < 5) ? 2:3;
        // 总分
        Integer PSQI = A+B+C+D+E+F+G;
        return PSQI;

    }

    private Integer countSleepEfficiency(String bedTime, String awakeTime, String sleepTime) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        long bedTimeL = format.parse(bedTime).getTime();
        long awakeTimeL = format.parse(awakeTime).getTime();
        long sleepTimeL = Long.parseLong(String.valueOf(sleepTime.charAt(0)));
        long onBedTimeL = awakeTimeL - bedTimeL;

        if (onBedTimeL<0){
            long zero = format.parse("00:00").getTime();
            long twoFour = format.parse("24:00").getTime();
            onBedTimeL = (twoFour - bedTimeL)+(awakeTimeL - zero);
        }

        long efficiency =  sleepTimeL / (onBedTimeL/60/60/1000) ;

        return efficiency >= 0.85 ? 0:efficiency >= 0.75 ? 1:efficiency >= 0.65 ? 2:3;
    }

    @Override
    public Double countSportScore(Map list) {
        // sport部分 42 43 44 45
        // 42 43 44 45
        // 0  1  2  3
        Double SPORT = 0d;
        Integer sport1;
        if ( list.get("42").equals("A") ){
            return SPORT;
        }else {
            sport1 = (list.get("42").equals("B") ) ? 1:(list.get("43").equals("C") ) ? 2:3;
        }
        Integer sport2 = (list.get("43").equals("A") ) ? 1:(list.get("43").equals("C") ) ? 2:(list.get("43").equals("D") )? 3: 0;;
        Integer sport3 = (list.get("44").equals("A") ) ? 1:(list.get("44").equals("B") ) ? 2:(list.get("44").equals("C") ) ? 3:(list.get("44").equals("D") )? 4: 0;
        Integer sport4 = (list.get("45").equals("A") ) ? 1:(list.get("45").equals("B") ) ? 1:(list.get("45").equals("C") ) ? 1:0;
        SPORT = (double) (sport1 + sport2 + sport3 + sport4);

        Double sportFinal = 0.0;
        if ( SPORT == 0 ){
            sportFinal = 3.0;
        }else if ( SPORT < 6 ){
            sportFinal = 3.0;
        }else if ( SPORT < 10 ){
            sportFinal = 4.5;
        }else{
            sportFinal = 6.0;
        }

        return sportFinal;
    }

}